package com.piz.service.orderservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;

import com.piz.dao.order.OrderDao;
import com.piz.model.BPO_Details;
import com.piz.model.BankDetails;
import com.piz.model.OrderDetails;
import com.piz.model.Ordertxn;
import com.piz.model.Payment_Type;
import com.piz.model.User;
import com.piz.model.common.OrderStatusEnum;
import com.piz.service.common.AppCache;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.model.Cart;
import com.piz.service.model.ConfirmOrderBean;

import com.piz.service.model.PizzaItem;
import com.piz.service.model.SideItem;
import com.piz.service.userservice.UserService;

public class PaymentService {
	
	@Autowired
	OrderDao orderDao;
	
	@Autowired
	UserService userService;
	
	public BankDetails getBankById(Long id)
	{
		return orderDao.findBankById(id);
	}
	
	public BPO_Details getBPOById(Long id)
	{
		return orderDao.findBPOById(id);
	}
	
	public void saveNewOrder(ConfirmOrderBean bean, String username)
	{
		Cart cart = (Cart) AppCache.getInstance().getCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId());
		Ordertxn order_txn = null;
		if(bean != null)
		{
			order_txn = new Ordertxn(cart.getGrandTotal(),new Double(0.0),OrderStatusEnum.NEW.getId(),
					bean.getBank_id(),new Long(0),new Date(),new Long(1),new Long(1),bean.getShipAddr(),bean.getPayment_type_id());
		}
		
		User user = null;
		try {
			user = userService.findUserByUsername(username);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		List<OrderDetails> orderItems = fetchOrderItems();
		order_txn.setOrderItem(orderItems);
		order_txn.setUser(user.getId());
		order_txn.setDiscountAmt(cart.getTotal_discount());
		orderDao.saveNewOrder(order_txn);
	}
	
	public List<OrderDetails> fetchOrderItems() {
		Cart cart = (Cart) AppCache.getInstance().getCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId());
		List<OrderDetails> orderItems = new ArrayList<OrderDetails>();
		if(cart.getPizzas() != null && cart.getPizzas().size()>0){
		for(PizzaItem p : cart.getPizzas())
		{
			OrderDetails o = new OrderDetails();
			o.setItemId(p.getPizzaId());
			o.setQuantity(p.getQuantity());
			o.setServeId(p.getServeId());
			o.setTotalPrice(p.getNetPrice());
			orderItems.add(o);
		}
		}
		if(cart.getSides() != null && cart.getSides().size()>0){
		for(SideItem s : cart.getSides())
		{
			OrderDetails o = new OrderDetails();
			o.setItemId(s.getId());
			o.setQuantity(s.getQuantity());
			o.setServeId(-1l);
			o.setTotalPrice(s.getNetPrice());
			orderItems.add(o);
		}
		}
		return orderItems;
	}
	
	public List<Payment_Type> fetchAllPaymentMethods() {
		return orderDao.fetchAllPaymentTypes();
	}
	
	public List<BankDetails> fetchAllBanks() {
		return orderDao.fetchAllBanks();
	}
}
